using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OA.Mvc3.Controllers
{
    [ValidateInput(false)]
    public class ResourceController :UI.BaseController
    {
        public const string ProgrameCode = "Resource";
        public static readonly string currentPath = UI.Utils.GetCurrentPath(ProgrameCode);

        public ResourceController()
        {
            ViewBag.CurrentPath = currentPath;
        }

        [UI.ValidateAuthority(ProgrameCode)]
        public ActionResult Index()
        {
            return View();
        }

        [UI.ValidateAuthority(ProgrameCode)]
        public string List(string id)
        {
            string websiteCode = null;
            string MenuCode = null;
            if (!string.IsNullOrEmpty(id))
            {
                var ids = id.Split('-');
                if (ids.Length > 0)
                {
                    websiteCode = ids[0];
                    if (ids.Length > 1) MenuCode = ids[1];
                }
            }
            List<object> listView = new List<object>();
            if (string.IsNullOrEmpty(websiteCode))
            {
                var weblist = new BLL.WebSite().Get(null,"Sort desc",0,0);
                foreach (var i in weblist)
                {
                    listView.Add(new
                    {
                        id = i.Code,
                        code = i.Code,
                        type = Common.EnumType.ResourceType.WebSite,
                        text = Server.HtmlEncode(i.Name),
                        state = i.Menus.Count > 0 ? "closed" : null,
                        iconCls = i.IconCls,
                        Sort = i.Sort,
                        IsShow = i.IsShow,
                        HrefUrl = i.Address
                    });
                }
            }
            else
            {
                var menulist = new BLL.Menu().GetByParent(websiteCode, MenuCode,null);
                foreach (var i in menulist)
                {
                    listView.Add(new
                    {
                        id = websiteCode + "-" + i.Code,
                        code=i.Code,
                        type = Common.EnumType.ResourceType.Menu,
                        text = Server.HtmlEncode(i.Name),
                        iconCls = i.IconCls,
                        state = i.Childs.Count + i.Buttons.Count > 0 ? "closed" : null,
                        Sort = i.Sort,
                        IsShow = i.IsShow,
                        HrefUrl = UI.Utils.GetMenuHrefUrl(i)
                    });
                }
                if (!string.IsNullOrEmpty(MenuCode))
                {
                    var buttonlist = new BLL.Button().GetByParent(websiteCode, MenuCode);
                    foreach (var i in buttonlist)
                    {
                        listView.Add(new
                        {
                            id = websiteCode +  "-" + MenuCode + "-" + i.Code,
                            code = i.Code,
                            type = Common.EnumType.ResourceType.Button,
                            text = Server.HtmlEncode(i.Name)
                        });
                    }
                }
            }
            return UI.Utils.ToJsonString(listView);
        }

        [UI.ValidateAuthority(ProgrameCode,"delete")]
        public string Delete(string id)
        {
            Common.Result ret = new Common.Result(false, 0, "删除失败");
            foreach (var i in id.Split(','))
            {
                string websiteCode = null;
                string MenuCode = null;
                string ButtonCode = null;
                if (!string.IsNullOrEmpty(id))
                {
                    var ids = i.Split('-');
                    if (ids.Length > 0)
                    {
                        websiteCode = ids[0];
                        if (ids.Length > 1)
                        {
                            MenuCode = ids[1];
                            if (ids.Length > 2) ButtonCode = ids[2];
                        }
                    }
                }
                if (!string.IsNullOrEmpty(ButtonCode))
                {
                    ret.count+= new BLL.Button().Delete(websiteCode, MenuCode, ButtonCode).count;
                }
                else if (!string.IsNullOrEmpty(MenuCode))
                {
                    ret.count += new BLL.Menu().Delete(websiteCode, MenuCode).count;
                }
                else
                {
                    ret.count += new BLL.WebSite().Delete(websiteCode).count;
                }
            }
            if (ret.count > 0)
            {
                ret.success = true;
                ret.message = "成功删除 " + ret.count.ToString() + " 条数据";
            }
            return UI.Utils.ToJsonString(ret);
        }

        [UI.ValidateAuthority(ProgrameCode, "add")]
        public ActionResult Add()
        {
            return View();
        }

        
        [HttpPost]
        [UI.ValidateAuthority(ProgrameCode, "add")]
        public string Add(Common.EnumType.ResourceType Type,Model.Menu model)
        {
            Common.Result ret=null;
            switch (Type)
            {
                case Common.EnumType.ResourceType.WebSite:
                    var website = new Model.WebSite()
                    {
                        Code = model.Code,
                        Name = model.Name,
                        Address = model.Address,
                        IconCls = model.IconCls,
                        IsShow = model.IsShow,
                        Sort = model.Sort
                    };
                    ret = new BLL.WebSite().Add(website);
                    break;
                case Common.EnumType.ResourceType.Menu:
                    ret = new BLL.Menu().Add(model);
                    break;
                case Common.EnumType.ResourceType.Button:
                    var button = new Model.Button()
                    {
                        Code = model.Code,
                        Name = model.Name,
                        WebSiteCode = model.WebSiteCode,
                        MenuCode = model.ParentCode
                    };
                    ret = new BLL.Button().Add(button);
                    break;
            }
            return UI.Utils.ToJsonString(ret);
        }

        [UI.ValidateAuthority(ProgrameCode, "edit")]
        public ActionResult Edit(string id)
        {
            string websiteCode = null;
            string MenuCode = null;
            string ButtonCode=null;
            if (!string.IsNullOrEmpty(id))
            {
                var ids = id.Split('-');
                if (ids.Length > 0)
                {
                    websiteCode = ids[0];
                    if (ids.Length > 1) {
                        MenuCode = ids[1];
                        if(ids.Length>2) ButtonCode=ids[2];
                    }
                }
            }
            Model.Menu model = new Model.Menu();
            if (!string.IsNullOrEmpty(ButtonCode))
            {
                var buttonModel = new BLL.Button().GetModel(websiteCode, MenuCode,ButtonCode);
                if (buttonModel == null) return UI.Utils.ErrorDateNoExist();
                ViewBag.Type = Common.EnumType.ResourceType.Button;
                model.Code = buttonModel.Code;
                model.Name = buttonModel.Name;
                model.ParentCode = buttonModel.MenuCode;
                model.WebSiteCode = buttonModel.WebSiteCode;                
            }
            else if (!string.IsNullOrEmpty(MenuCode))
            {
                var menumodel = new BLL.Menu().GetModel(websiteCode, MenuCode);
                if (menumodel == null) return UI.Utils.ErrorDateNoExist();
                ViewBag.Type = Common.EnumType.ResourceType.Menu;
                model = menumodel;
            }
            else
            {
                var webModel = new BLL.WebSite().GetModel(websiteCode);
                if (webModel == null) return UI.Utils.ErrorDateNoExist();
                ViewBag.Type = Common.EnumType.ResourceType.WebSite;
                model.Code = webModel.Code;
                model.Name = webModel.Name;
                model.Address = webModel.Address;
                model.IconCls = webModel.IconCls;
                model.IsShow = webModel.IsShow;
                model.Sort = webModel.Sort;
            }
            return View(model);
        }

        [HttpPost]
        [UI.ValidateAuthority(ProgrameCode, "edit")]
        public string Edit(Common.EnumType.ResourceType Type, Model.Menu model)
        {
            Common.Result ret = null;
            switch (Type)
            {
                case Common.EnumType.ResourceType.WebSite:
                    var website = new Model.WebSite()
                    {
                        Code = model.Code,
                        Name = model.Name,
                        Address = model.Address,
                        IconCls = model.IconCls,
                        IsShow = model.IsShow,
                        Sort = model.Sort
                    };
                    ret = new BLL.WebSite().Update(website);
                    break;
                case Common.EnumType.ResourceType.Menu:
                    ret = new BLL.Menu().Update(model);
                    break;
                case Common.EnumType.ResourceType.Button:
                    var button = new Model.Button()
                    {
                        Code = model.Code,
                        Name = model.Name,
                        WebSiteCode = model.WebSiteCode,
                        MenuCode = model.ParentCode
                    };
                    ret = new BLL.Button().Update(button);
                    break;
            }
            return UI.Utils.ToJsonString(ret);
        }

        public string WebSiteCombo()
        {
            List<object> ret = new List<object>();
            var list=new BLL.WebSite().Get(null, null, 0, 0);
            foreach (var i in list)
            {
                ret.Add(new
                {
                     id=i.Code,
                     text=i.Name
                });
            }
            return UI.Utils.ToJsonString(ret);
        }

        public string MenuTree(string websiteCode, string id)
        {
            var ret = new BLL.Menu().GetByParent(websiteCode, id, null);
            List<object> trees = new List<object>();
            if (id == null)
            {
                trees.Add(new
                {
                    id = "",
                    text = "　",
                });
            }
            foreach (var i in ret)
            {
                trees.Add(new
                {
                    id = i.Code,
                    text = Server.HtmlEncode(i.Name),
                    state = i.Childs.Count > 0 ? "closed" : null,
                    iconCls = i.IconCls
                });
            }
            return UI.Utils.ToJsonString(trees);
        }
    }
}
